package com.mdl.springmvc.ioStream.io;

import java.io.*;
import java.util.HashMap;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/12/28 17:19
 */
public class BufferedTest06 {
    private static final String PATH = "G:\\dhcc\\idea\\MavenProject\\src\\main\\webapp\\files\\";
    public static void main(String[] args) {
        // 创建map集合,保存文本数据,键为序号,值为文字
        HashMap<String, String> lineMap = new HashMap<>();

        //字符缓冲流
        //创建流对象  源
        BufferedReader br = null;
        //目标
        BufferedWriter bw = null;
        //字节缓冲流
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            br = new BufferedReader(new FileReader(PATH+"aa.txt"));
            bw = new BufferedWriter(new FileWriter(PATH+"bb.txt"));
            bis = new BufferedInputStream(new FileInputStream(PATH+"aa.txt"));
            bos = new BufferedOutputStream(new FileOutputStream(PATH+"bb.txt"));

            // 读取数据
            String line  = null;
            //readLine 读一行数据 字符缓冲流特有方法
            while ((line = br.readLine())!=null) {
                // 解析文本
                String[] split = line.split("\\.");
                // 保存到集合
                lineMap.put(split[0],split[1]);
            }

            // 遍历map集合
            for (int i = 1; i <= lineMap.size(); i++) {
                String key = String.valueOf(i);
                // 获取map中文本
                String value = lineMap.get(key);
                // 写出拼接文本
                bw.write(key+"."+value);
                //newLine 换行 字符缓冲流特有方法
                bw.newLine();
            }
            System.out.println("================");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                br.close();
                bw.close();
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
